//给定一个整数，打印该整数的英文描述。 
//
// 示例 1： 
//
// 
//输入：123
//输出："One Hundred Twenty Three"
// 
//
// 示例 2： 
//
// 
//输入：12345
//输出："Twelve Thousand Three Hundred Forty Five" 
//
// 示例 3： 
//
// 
//输入：1234567
//输出："One Million Two Hundred Thirty Four Thousand Five Hundred Sixty Seven" 
//
// 示例 4： 
//
// 
//输入：1234567891
//输出："One Billion Two Hundred Thirty Four Million Five Hundred Sixty Seven 
//Thousand Eight Hundred Ninety One" 
//
// 注意：本题与 273 题相同：https://leetcode-cn.com/problems/integer-to-english-words/ 
//
// Related Topics 递归 数学 字符串 👍 27 👎 0


package LeetCode.editor.cn;

/**
 * @author ldltd
 * @date 2025-09-07 13:10:40
 * @description 面试题 16.08.整数的英语表示
 */
public class EnglishIntLcci{
	 public static void main(String[] args) {
	 	 //测试代码
	 	 EnglishIntLcci fun=new EnglishIntLcci();
	 	 Solution solution = fun.new Solution();

	 }
	 
//力扣代码
//leetcode submit region begin(Prohibit modification and deletion)
class Solution {
		 // 1,234,567,891
	 String[] low = {"","One","Two","Three","Four","Five","Six","Seven","Eight","Nine"};
	String[] mid = {"Ten","Eleven","Twelve","Thirteen","Fourteen","Fifteen","Sixteen","Seventeen","Eighteen","Nineteen"};
	String[] high = {"","","Twenty","Thirty","Forty","Fifty","Sixty","Seventy","Eighty","Ninety"};
    public String numberToWords(int num) {
		if(num==0){
			return "Zero";
		}
		int part1 = num%1000;
		num = num/1000;
		int part2 = num%1000;
		num = num/1000;
		int part3 = num%1000;
		num = num/1000;
		int part4 = num;
		StringBuilder ans = new StringBuilder();
		if(part4!=0){
			ans.append(buildNumber(part4)).append("Billion ");
		}
		if(part3!=0){
			ans.append(buildNumber(part3)).append("Million ");
		}
		if(part2!=0){
			ans.append(buildNumber(part2)).append("Thousand ");
		}
		if(part1!=0){
			ans.append(buildNumber(part1));
		}
		return ans.toString().trim();
    }
	// 构建一个三位数
	public StringBuilder buildNumber(int num){
		int a = num % 10;        // 个位
		num = num / 10;
		int b = num % 10;        // 十位
		num = num / 10;
		int c = num;             // 百位
		StringBuilder ans = new StringBuilder();
		// 处理百位
		if(c!=0){
			ans.append(low[c]).append(" Hundred");
		}
		// 处理十位和个位
		if(b==1){
			if (ans.length() > 0) ans.append(" ");
			ans.append(mid[a]);
		}else if(b>1){
			if (ans.length() > 0) ans.append(" ");
			ans.append(high[b]);
			if (a != 0) {
				ans.append(" ").append(low[a]);
			}
		}else if (a != 0) { // b == 0
			if (ans.length() > 0) ans.append(" ");
			ans.append(low[a]);
		}
		// 只在有内容的情况下添加空格
		if (ans.length() > 0) {
			ans.append(" ");
		}
		return ans;
	}
}
//leetcode submit region end(Prohibit modification and deletion)

}
